package yagwl.client.view.demo;

import yagwl.client.model.demo.ProductListCache;
import yagwl.client.view.fnd.AbstractEntryPoint;
import yagwl.client.view.fnd.FlexEntityView;
import yagwl.client.widget.PageLayout;
import yagwl.client.widget.internal.YClientBundle;

import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.RunAsyncCallback;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.RootPanel;

public class YAGWLDemo extends AbstractEntryPoint {
  static {
    YClientBundle.I.css().ensureInjected();
  }

  private PageLayout rootPL = new PageLayout("240px", "100px");
  private ProductHierarchy productHierarchy;
  private ProductListView productListView;
  private ProductEditForm productEditForm;
  private LaureateListView laureateListView;
  private LaureateListView2 laureateListView2;
  private FilteredLaureateView filteredListView;
  private ContactCreateForm createContactForm;
  private FlexEntityView flexEntityView;
  private ProductListCache listCache;

  @Override
  protected void onModuleLoad2() {
    GlobalHeader globalHeader = new GlobalHeader();
    rootPL.setGlobalHeader(globalHeader);
    rootPL.setLeftColumn(new HorizontalPanel());
    rootPL.setCenterColumn(new HorizontalPanel());
    rootPL.renderUI();
    RootPanel.get("yagwl-demo").add(rootPL);

    GWT.runAsync(new RunAsyncCallback() {

      @Override
      public void onFailure(Throwable reason) {
        Window.alert("Code download failed");
      }

      @Override
      public void onSuccess() {
        SideMenu sideMenu = new SideMenu();
        rootPL.setLeftColumn(sideMenu);
      }

    });

    if (History.getToken().length() == 0) {
      History.newItem(LaureateListView2.HistoryToken, false);
    }
    History.fireCurrentHistoryState();

  }

  @Override
  protected void changeView(final String historyToken) {
    String h = historyToken;
    int index = h.indexOf("/");
    if (index >= 0)
      h = h.substring(0, index);
    final String token = h;

    GWT.runAsync(new RunAsyncCallback() {

      @Override
      public void onFailure(Throwable reason) {
        Window.alert("Code download failed");
      }

      @Override
      public void onSuccess() {
        if (token.equals(ProductHierarchy.HistoryToken)) {
          if (listCache == null)
            listCache = new ProductListCache();
          if (productHierarchy == null)
            productHierarchy = new ProductHierarchy(listCache);
          rootPL.setCenterColumn(productHierarchy);
          return;
        }
        if (token.equals(ProductListView.HistoryToken)) {
          if (productEditForm == null)
            productEditForm = new ProductEditForm();
          if (productListView == null) {
            if (listCache == null)
              listCache = new ProductListCache();
            productListView = new ProductListView(listCache, productEditForm);
          }
          rootPL.setCenterColumn(productListView);
          return;
        }
        if (token.equals(ProductEditForm.HistoryToken)) {
          if (productEditForm == null)
            productEditForm = new ProductEditForm();
          rootPL.setCenterColumn(productEditForm);
          return;
        }
        if (token.equals(LaureateListView.HistoryToken)) {
          if (laureateListView == null)
            laureateListView = new LaureateListView();
          rootPL.setCenterColumn(laureateListView);
          return;
        }
        if (token.equals(LaureateListView2.HistoryToken)) {
          if (laureateListView2 == null)
            laureateListView2 = new LaureateListView2();
          rootPL.setCenterColumn(laureateListView2);
          return;
        }
        if (token.equals(FilteredLaureateView.HistoryToken)) {
          if (filteredListView == null)
            filteredListView = new FilteredLaureateView();
          rootPL.setCenterColumn(filteredListView);
          return;
        }
        if (token.equals(ContactCreateForm.HistoryToken)) {
          if (createContactForm == null)
            createContactForm = new ContactCreateForm();
          rootPL.setCenterColumn(createContactForm);
          return;
        }
        if (token.equals(FlexEntityView.HistoryToken)) {
          if (flexEntityView == null)
            flexEntityView = new FlexEntityView();
          rootPL.setCenterColumn(flexEntityView);
          return;
        }
      }
    });
  }

}
